﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Dynamic;
using Models;
using Newtonsoft.Json;

public partial class Admin_News_choose : AdminBase
{
    Entities db = new Entities();
    HashSet<int> selects_ids = new HashSet<int>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ids"]))
            {
                var ids = Request.QueryString["ids"].Split(',');
                if (ids.Any())
                {
                    int i = 0;
                    foreach (var id in ids)
                    {
                        if (int.TryParse(id, out i))
                        {
                            selects_ids.Add(i);
                        }
                    }
                }
            }
        }
        jsTreeJson.Text = getTree();
    }

    protected string getTree()
    {
    
        dynamic JsTree = new ExpandoObject();
        var list = new List<object>();

        int classid = 6445;
        var m =db.L_Newss;

        var myNew = PredicateBuilder.True<L_Newss>();
            myNew = myNew.And(p => p.ClassId == classid);
            var subclasss = db.L_Newss.Where(x => x.NSubClassId != null && x.NSubClassId != "").ToList();
            {
                foreach (var xSub in subclasss)
                {
                    if (xSub.NSubClassId != null)
                    {
                        string xxClassId = xSub.NSubClassId.ToString();

                        if (xxClassId.IndexOf("," + classid + ",") > -1)
                        {
                            myNew = myNew.Or(p => p.NID == xSub.NID);
                        }
                    }
                }
            }


      var xx= m.Where(myNew.Compile()).OrderByDescending(x => x.NID).Distinct().ToList();


        foreach (var n in xx)
        {

            list.Add(new
            {
                id = "node_" + n.NID,
                parent = "#",
                text = n.Title,
                state = new
                {
                    opened = true,
                    selected = selects_ids.Contains(n.NID)
                },
                a_attr = new
                {
                    link = "#"
                }
            });
        
          
        }
        JsTree.data = list;
        return JsonConvert.SerializeObject(JsTree);
    }

 
}



